package cdu.xjj.controller;

import cdu.xjj.pojo.Result;
import cdu.xjj.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    /**
     * 本地储存的上传文件
     */
   /* @PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile file) throws IOException {
        log.info("上传信息：{}，{}，{}" , name, age, file);
        
        String originalFilename = file.getOriginalFilename();

        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString()+ extension;
        file.transferTo(new File("D:/images/"+newFileName));
        return Result.success();
    }*/

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("本地文件上传的原地址:{}",file.getOriginalFilename());
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("保存到阿里云之后的文件路径:{}", url);
        return Result.success(url);
    }

}
